package com.qianxun.module.datasource1.common.service;

import com.qianxun.init.properties.AliyunOssProperties;
import com.qianxun.module.datasource1.common.util.AliyunOssStore;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 阿里云oss服务
 * @author: huwei
 * @date: 2019/12/6 15:10
 * @version: 1.0.0
 */
@Service
public class AliyunOssService {

    private AliyunOssStore aliyunOssStore;

    private AliyunOssProperties aliyunOssProperties;

    public AliyunOssService(AliyunOssProperties aliyunOssProperties){
        this.aliyunOssStore = new AliyunOssStore(aliyunOssProperties.getEndpoint() ,aliyunOssProperties.getAccessKeyId(),aliyunOssProperties.getAccessKeySecret(),aliyunOssProperties.getBucketName(),aliyunOssProperties.getCdn());
        this.aliyunOssProperties = aliyunOssProperties;
    }

    /**
     * 上传资源
     * @param ossKey 资源key
     * @param file 资源文件
     * @return true 上传成功， false 上传失败
     */
    public boolean upload(String ossKey ,File file){
        String res = aliyunOssStore.uploadFileToOSS(file ,wrapperOssKey(ossKey));
        return res != null;
    }
    /**
     * 上传资源
     * @param ossKey 资源key
     * @param buf 资源缓存
     * @return true 上传成功， false 上传失败
     */
    public boolean upload(String ossKey , byte[] buf){
        String res = aliyunOssStore.uploadFileToOSS(buf ,wrapperOssKey(ossKey));
        return res != null;
    }
    /**
     * 上传资源
     * @param ossKey 资源key
     * @param is 资源输入流
     * @return true 上传成功， false 上传失败
     */
    public boolean upload(String ossKey,InputStream is){
        String res = aliyunOssStore.uploadFileToOSS(is ,wrapperOssKey(ossKey));
        return res != null;
    }
    /**
     * 下载指定资源
     * @param ossKey 资源key
     * @return
     */
    public byte[] download(String ossKey){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        aliyunOssStore.downOutputStream(byteArrayOutputStream ,wrapperOssKey(ossKey));
        byte[] buf = byteArrayOutputStream.toByteArray();
        try {
            byteArrayOutputStream.close();
        }catch (IOException e){}
        return buf;
    }
    /**
     * 删除指定资源
     * @param ossKey 资源key
     */
    public void delete(String ossKey){
        aliyunOssStore.deleteFileFromOSS(wrapperOssKey(ossKey));
    }

    /**
     * 批量删除指定资源
     * @param ossKeyList
     */
    public void batchDelete(List<String> ossKeyList){

        aliyunOssStore.deleteFileFromOSS(wrapperOssKey(ossKeyList));
    }

    /**
     * 下载地址
     * @param ossKey 资源key，用于标识存储于阿里云oss的资源；格式：a/b/c/d/1.txt
     * @return
     */
    public String downloadUrl(String ossKey){
        return aliyunOssProperties.getCdn().concat("/").concat(wrapperOssKey(ossKey));
    }
    /**
     * 封装ossKey，为上传资源添加统一的根目录
     * @param ossKey 资源key
     * @return
     */
    private String wrapperOssKey(String ossKey){
        StringBuffer sb = new StringBuffer();
        sb.append(aliyunOssProperties.getRoot()).append("/").append(ossKey);
        return sb.toString();
    }

    private List<String> wrapperOssKey(List<String> ossKeyList){
        List<String> res = new ArrayList<>(ossKeyList.size());
        ossKeyList.forEach(x->{
            res.add(wrapperOssKey(x));
        });
        return res;
    }
}
